package src.CodeTopSecondPractice;

public class _92 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode virNode = new ListNode(0);
        ListNode mark = virNode;
        virNode.next = head;
        int count = right - left + 1;
        for (int i = 0 ; i < left - 1 ; i++) {
            virNode = virNode.next;
        }
        virNode.next = reverseN(virNode.next, count);
        return mark.next;
    }

    public static ListNode reverseN(ListNode head, int n) {
        if (n == 1)
            return head;
        ListNode tail = head.next;
        ListNode p = reverseN(head.next, n - 1);
        head.next = tail.next;
        tail.next = head;
        return p;
    }
}
